package eclipse.util;
import javax.script.ScriptException;

import eclipse.lib.scripting.JScript;
/**
 *  A basic thread interface that has CPU limitations.
 * Interval represents how many ms delay between executions
 * setRunStatus(false); will kill the thread
 * setEnabled(true); is required for running.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
	public class ScriptedThreadedTimer extends Thread
	{
		private long 	Interval	= 1000;
		private boolean Enabled		= false;
		private boolean isRunning 	= false;
		private boolean Scripted 	= false;
		private String 	Script 		= "";
		/**
		 * Sets how frequent the thread is executed
		 * @param lInterval Frequency in milliseconds
		 */
		public void setInterval(long lInterval)
		{
			Interval = lInterval;
		}
		/**
		 * Returns the frequency of execution
		 * @return
		 */
		public long getInterval()
		{
			return Interval;
		}
		/**
		 * Enables the execution of the Related code
		 * @param bEnabled
		 */
		public void setEnabled(boolean bEnabled)
		{
			Enabled = bEnabled;
		}
		/**
		 * Returns if the Timer is enabled.
		 */
		public boolean getEnabled()
		{
			return Enabled;
		}
		/**
		 * Sets the run status of the Thread.
		 * false = Shuts down the thread
		 */
		public void setRunStatus(boolean bStatus)
		{
			isRunning = bStatus;
		}
		/**
		 * Returns the run status of the Thread.
		 * Same as isAlive();
		 */
		public boolean getRunStatus()
		{
			return isRunning;
		}
		/** 
		 * Sets whether the thread will execute a script
		 */
		public void setScripted(boolean bScripted)
		{
			Scripted = bScripted;
		}
		/**
		 * Returns whether the thread executes a script.
		 */
		public boolean getScripted()
		{
			return Scripted;
		}
		/**
		 * Sets the thread to execute a script.
		 * @param sScript The script the thread will execute
		 */
		public void setScript(String sScript)
		{
			Script = sScript;	
		}
		/**
		 * Returns the script that the thread executes
		 * @return
		 */
		public String getScript()
		{
			return Script;
		}
		/**
		 * Creates a ThreadedTimer object
		 */
		public ScriptedThreadedTimer()
		{
			isRunning 	= true;
		}
		/**
		 * Creates a ThreadedTimer object
		 * @param iInterval Frequency of execution
		 * @param Script Script executed
		 */
		public ScriptedThreadedTimer(int iInterval, String Script)
		{
			Interval	= iInterval;
			isRunning 	= true;
			Scripted 	= true;
		}
		/**
		 * Creates a ThreadedTimer object
		 * @param iInterval Frequency of execution
		 */
		public ScriptedThreadedTimer(int iInterval)
		{
			Interval 	= iInterval;
			isRunning 	= true;		
		}
		public void run()
		{
			while(isRunning)
			{
				if(Enabled && Scripted)
				{
					try {
						JScript.run(Script);
					} catch (ScriptException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				Sleep(Interval);
			}
			System.out.println("Ending Thread: " + getName());
		}
		/**
		 * Pauses the thread
		 * @param ms Miliseconds for pause
		 */
		public void Sleep(long ms)
		{
			try {
				sleep(ms);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
